void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
{
	(void)sensor_id;  // Ignore parameter sensor_id

	HAL_GPIO_WritePin(ENABLE_GPIO_Port, ENABLE_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_SET);

	// Wait 2 ms to make sure that the sensor crystal have time to stabilize
	delay_ms(2);
}


void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
{
	(void)sensor_id;  // Ignore parameter sensor_id
	HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(ENABLE_GPIO_Port, ENABLE_Pin, GPIO_PIN_RESET);

	// Wait after disable to leave the sensor in a known state
	// in case the application intends to enable the sensor directly
	delay_ms(2);
}

const acc_hal_a121_t *acc_hal_rss_integration_get_implementation(void)
{
	static const acc_hal_a121_t val =
	{
		.max_spi_transfer_size = STM32_MAX_TRANSFER_SIZE,

		.mem_alloc = malloc,
		.mem_free  = free,

		.transfer = acc_hal_integration_sensor_transfer8,
		.log      = acc_integration_log,

		.optimization.transfer16 = NULL,
	};

	return &val;
}
bool acc_hal_integration_wait_for_sensor_interrupt(acc_sensor_id_t sensor_id, uint32_t timeout_ms)
{
	(void)sensor_id; // Ignore parameter sensor_id

	const uint32_t wait_begin_ms = HAL_GetTick();
	while ((HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) != GPIO_PIN_SET) &&
	       (HAL_GetTick() - wait_begin_ms < timeout_ms))
	{
		// Wait for the GPIO interrupt
		disable_interrupts();
		// Check again so that IRQ did not occur
		if (HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) != GPIO_PIN_SET)
		{
			__WFI();
		}

		// Enable interrupts again to allow pending interrupt to be handled
		enable_interrupts();
	}

	return HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) == GPIO_PIN_SET;
}
static void acc_hal_integration_sensor_transfer8(acc_sensor_id_t sensor_id, uint8_t *buffer, size_t buffer_length)


{
	(void)sensor_id;  // Ignore parameter sensor_id
	// Set SPI_SS LOW (Activate)
	HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_RESET);

	//const uint32_t SPI_TRANSMIT_RECEIVE_TIMEOUT = 5000;

#ifdef A121_USE_SPI_DMA
	spi_transfer_complete = false;
	HAL_StatusTypeDef status = HAL_SPI_TransmitReceive_DMA(&A121_SPI_HANDLE, (uint8_t *)buffer, (uint8_t *)buffer, buffer_length);

	if (status != HAL_OK)
	{
		return;
	}

	uint32_t start = HAL_GetTick();

	while (!spi_transfer_complete && (HAL_GetTick() - start) < SPI_TRANSMIT_RECEIVE_TIMEOUT)
	{
		// Turn off interrupts
		disable_interrupts();
		// Check once more so that the interrupt have not occurred
		if (!spi_transfer_complete)
		{
			__WFI();
		}

		// Enable interrupt again, the ISR will execute directly after this
		enable_interrupts();
	}
#else
	HAL_SPI_TransmitReceive_IT(&A121_SPI_HANDLE, (uint8_t *)buffer, (uint8_t *)buffer, buffer_length);
	while(A121_SPI_HANDLE.State!=HAL_SPI_STATE_READY && A121_SPI_HANDLE.State!=HAL_SPI_STATE_ERROR);
#endif

	// Set SPI_SS HIGH (De-activate)
	HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_SET);
}

